package org.koon;

import cn.hutool.core.util.StrUtil;
import org.koon.jadelyre.KoonPacket;

import java.util.HashMap;
import java.util.Map;

public class KooRequest extends KoonPacket {

    private RequestLine requestLine;
    private Map<String, String> headers = new HashMap<>();
    private int contentLength;
    private Map<String, Object[]> params = new HashMap<>();
    private BodyFormat bodyFormat;
    private String bodyString;
    private Map<String,Cookie> cookieMap = new HashMap<>();
    private String charset = super.DEFAULT_CHARSET;

    //---
    private Map<String, KoonFile> koonFileMap;

    public RequestLine getRequestLine() {
        return requestLine;
    }

    public void setRequestLine(RequestLine requestLine) {
        this.requestLine = requestLine;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }

    public int getContentLength() {
        return contentLength;
    }

    public void setContentLength(int contentLength) {
        this.contentLength = contentLength;
    }

    public Map<String, Object[]> getParams() {
        return params;
    }

    public void setParams(Map<String, Object[]> params) {
        this.params = params;
    }

    public BodyFormat getBodyFormat() {
        return bodyFormat;
    }

    public void setBodyFormat(BodyFormat bodyFormat) {
        this.bodyFormat = bodyFormat;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public String getBodyString() {
        return bodyString;
    }

    public void setBodyString(String bodyString) {
        this.bodyString = bodyString;
    }

    public void addParam(String key, Object value) {
        if (value == null) {
            return;
        }
        Object[] existValue = params.get(key);
        if (existValue != null) {
            Object[] newExistValue = new Object[existValue.length + 1];
            if (value instanceof String) {
                newExistValue = new String[existValue.length + 1];
            } else if (value instanceof KoonFile) {
                newExistValue = new KoonFile[existValue.length + 1];
            }
            System.arraycopy(existValue, 0, newExistValue, 0, existValue.length);
            newExistValue[newExistValue.length - 1] = value;
            params.put(key, newExistValue);
        } else {
            Object[] newExistValue = null;//new Object[] { value };
            if (value instanceof String) {
                newExistValue = new String[] { (String) value };
            } else if (value instanceof KoonFile) {
                newExistValue = new KoonFile[] { (KoonFile) value };
            }
            params.put(key, newExistValue);
        }
    }

    public void addKoonFile(KoonFile koonFile){
        if (koonFileMap == null){
            koonFileMap = new HashMap<>();
        }
        koonFileMap.put(koonFile.getName(),koonFile);
    }
    public KoonFile getKoonFile(String name){
        if (koonFileMap == null){
            return null;
        }
        return koonFileMap.get(name);
    }

    public Object getParam(String name){
        Object[] objects = params.get(name);
        if (objects == null){
            return null;
        }
        return objects[0];
    }

    public void parseCookie(){
        String s = headers.get(Header.COOKIE.getValue().toLowerCase());
        if (StrUtil.isBlank(s)){
            return;
        }
        String[] split = s.split(";");
        for (String s1 : split) {
            String[] split1 = s1.trim().split("=",2);
            if (split1.length < 2){
                continue;
            }
            cookieMap.put(split1[0],new Cookie(split1[0],split1[1]));
        }
    }

    public Cookie getCookie(String name){
        return cookieMap.get(name);
    }
}
